package com.vcredit.creditcat.manager;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IMessageManager;
import com.vcredit.creditcat.dto.MessageDto;
import com.vcredit.creditcat.enumtype.MessageOperationEnum;
import com.vcredit.creditcat.exception.DataException;
import com.vcredit.creditcat.model.Message;
import com.vcredit.creditcat.service.MessageService;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.BeanUtil;
import com.vcredit.framework.util.BeanUtilFilter;

@Service(protocol = { "protocol1" }, version = "1.0")
public class MessageManager implements IMessageManager {
	@Autowired
	private MessageService messageService;

	@Log
	@Override
	public List<MessageDto> getMessageList(Long custId, Integer msgKind, Integer pageIndex, Integer pageSize)
			throws Exception {
		List<Message> list = messageService.findByCustId(custId, msgKind, pageIndex, pageSize);
		return BeanUtil.copy(list, MessageDto.class, new BeanUtilFilter<MessageDto, Message>() {
			@Override
			public MessageDto filter(Message source, MessageDto target) {
				target.setTitle(source.getMsgTitle());
				target.setContent(source.getMsgContent());
				target.setTime(source.getCreateTime() == null ? 0 : source.getCreateTime().getTime());
				return target;
			}
		});
	}

	@Log
	@Override
	public void modifyMessages(List<Long> msgIds, MessageOperationEnum msgOperation, int msgKind) throws Exception {
		// 设置消息修改mode
		Message msgMode = new Message();
		Message msgParam = new Message();
		Collections.reverse(msgIds);
		// 如果不为空，则进行操作之前的数据
		if (StringUtils.isNotBlank(msgKind + "")) {

			switch (msgOperation) {
			case READ:
				msgMode.setMsgType(msgKind);
				msgMode.setIsRead(true);
				msgMode.setReadTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				
				
				msgParam.setMsgType(msgKind);
				msgParam.setIsRead(false);
				break;
			case DELETE:
				msgMode.setIsDelete(true);
				msgMode.setDeleteTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				msgParam.setMsgType(msgKind);
				msgParam.setIsDelete(true);
				break;
			default:
				// msgMode.setRemark("执行批量处理异常，执行操作为："+msgOperation.getDesc());
				throw new DataException("错误的消息操作类型：" + msgOperation);
			}
			msgParam.setMsgId(msgIds.get(0));
			//首页消息 
			if(4==msgKind){
				// 查询未删除之前的数据
				List<Message> listMsg = messageService.dataBeforeMsg(msgParam);
				for (Message message : listMsg) {
					msgIds.add(message.getMsgId());
				}
			}
		} else {
			// 操作该条数据
			switch (msgOperation) {
			case READ:
				msgMode.setIsRead(true);
				msgMode.setReadTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				break;
			case DELETE:
				msgMode.setIsDelete(true);
				msgMode.setDeleteTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				break;
			default:
				// msgMode.setRemark("执行批量处理异常，执行操作为："+msgOperation.getDesc());
				throw new DataException("错误的消息操作类型：" + msgOperation);
			}
		}
		messageService.modifyMessageByOperation(msgIds, msgMode);

	}

	@Log
	@Override
	public Integer getUnreadCount(Long custId) throws Exception {
		List<Message> unreads = messageService.findUnreadByCustId(custId);
		if (unreads == null || unreads.isEmpty()) {
			return 0;
		}
		return unreads.size();
	}

	@Override
	public void sendNotify(Long custId, String title, String content) throws Exception {

	}
}
